package org.javaforever.cookieshop.controller;

import java.util.Map;
import java.util.TreeMap;

import org.javaforever.cookieshop.domain.GoodsBean;
import org.javaforever.cookieshop.service.CustOrderService;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.javaforever.cookieshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminGoodsCustRestController { 
	@Autowired
	protected GoodsService goodsService;
	@Autowired
	protected CustOrderService orderService;
	@Autowired
	protected TypeService typeService;
	@Autowired
	protected UserService userService;

    @PostMapping("/admin/goods_edit")
    public Map<String,Object> adminGoodsEdit(@RequestBody GoodsBean goods, Model model) throws Exception{   
        Map<String,Object> result = new TreeMap<String,Object>();
        GoodsBean oriGoods = goodsService.findGoodsById(goods.getId());
        oriGoods.setName(goods.getName());
        oriGoods.setPrice(goods.getPrice());
        oriGoods.setIntro(goods.getIntro());
        oriGoods.setStock(goods.getStock());
        oriGoods.setType(goods.getType());
        if (GoodsController.tempGoods!=null && GoodsController.tempGoods.getCover()!=null) {
        	oriGoods.setCover(GoodsController.tempGoods.getCover());
        }
        if (GoodsController.tempGoods!=null && GoodsController.tempGoods.getImage1()!=null) {
        	oriGoods.setImage1(GoodsController.tempGoods.getImage1());
        }
        if (GoodsController.tempGoods!=null && GoodsController.tempGoods.getImage2()!=null) {
        	oriGoods.setImage2(GoodsController.tempGoods.getImage2());
        }
        GoodsController.tempGoods = new  GoodsBean();
        goodsService.updateGoods(oriGoods);
        result.put("success",true);
        result.put("data",null);
        return result;
 	}
 }
